package com.fuda.trade.party3.cczq

import android.graphics.Rect
import android.util.Log
import com.fuda.trade.model.TradeOrderData
import com.fuda.trade.Party3AppManager
import com.fuda.trade.TradeAccessibilityService
import com.fuda.trade.TradeAccessibilityService.Companion.PARTY3_APP_TIMEOUT_MS
import com.fuda.trade.logElapsedTime
import com.fuda.trade.model.TradeOrder
import com.fuda.trade.startParty3App

class CczqAppHandler : Party3AppManager.Party3AppHandler {

    companion object{
        val TAG: String = CczqAppHandler::class.java.simpleName
    }

    override fun isAppActive(service: TradeAccessibilityService): Boolean{
        var result = false

        result = service.isParty3AppActive("com.tdx.androidCCZQ", PARTY3_APP_TIMEOUT_MS)
        return result
    }

    private fun startApp(service: TradeAccessibilityService): Boolean {
        var result = service.globalGoHome()
        if (result) {
            // 1. 初始化启动应用
            result = startParty3App(service.applicationContext,"com.tdx.androidCCZQ", "com.tdx.Android.TdxAndroidActivity")
        }
        return result
    }

    override fun handleOrder(service: TradeAccessibilityService, tradeOrder: TradeOrder):Boolean {
        val startTime = System.currentTimeMillis()
        var result = false
        tradeOrder.data?.let {
            when (it){
                is TradeOrderData.Buy -> {
                    if (it.isAuto==true){
                        Log.i(TAG, "handleOrder auto tradeOrder= $tradeOrder")
                        result = startApp(service)
                        if (result) {
                            result = touchBottomNavHomeButton(service)
                        }
                        if (result) {
                            result = clickBottomNavTradeButton(service)
                        }
                        if (result) {
                            result = clickBuyMenuItem(service)
                        }
                        if (result) {
                            result = editTradeAccountNumber(service)
                        }else{

                        }
                    }else{
                        Log.e(TAG, "Please ask user to confirm, Here should only handle auto order: $tradeOrder")
                        service.startMainActivity("ASK_CONFIRM_MANUAL_TRADE_ORDER", tradeOrder.serialize())
                    }

                }
                is TradeOrderData.Sell ->  {
                    if (it.isAuto==true) {
                        Log.i(TAG, "handleOrder tradeOrderData= $tradeOrder")
                    }else{
                        Log.e(TAG, "Please ask user to confirm, Here should only handle auto order: $tradeOrder")
                        service.startMainActivity("ASK_CONFIRM_MANUAL_TRADE_ORDER", tradeOrder.serialize())
                    }
                }
            }
        }

        logElapsedTime(TAG, startTime)
        return result
    }

    override fun refreshActiveForeground(service: TradeAccessibilityService):Boolean {
        val startTime = System.currentTimeMillis()
        Log.e(TAG, "refreshActiveForeground...")
        var result = startApp(service)
        if (result) {
            result = touchBottomNavHomeButton(service)
        }
        if (result) {
            result = clickBottomNavTradeButton(service)
        }
        if (result) {
            result = clickBuyMenuItem(service)
        }
        if (result) {
            result = editTradeAccountNumber(service)
        }
        logElapsedTime(TAG, startTime)
        return result
    }

    private fun touchBottomNavHomeButton(service: TradeAccessibilityService) :Boolean{
        // 1. 自动触摸【首页】底部按钮
        // ______2, null, android.widget.FrameLayout, null, 0,0,2,, [0, 2097, 216, 2244], true, false,
        // ________0, null, android.widget.ImageButton, null, 0,0,2,0,, [69, 2102, 147, 2180], false, false,
        // ________1, null, android.widget.TextView, [首页], 0,0,2,1,, [65, 2182, 151, 2241], false, false,

        return  service.waitForTextAndTouch(Rect(0, 2097, 216, 2244),"首页", 10*1000)
    }

    private fun clickBottomNavTradeButton(service:TradeAccessibilityService):Boolean{
        // 2. 自动点击【交易】底部按钮
        // ______4, null, android.widget.FrameLayout, null, 0,0,4,, [432, 2097, 648, 2244], true, false,
        // ________0, null, android.widget.ImageButton, null, 0,0,4,0,, [501, 2102, 579, 2180], false, false,
        // ________1, null, android.widget.TextView, [交易], 0,0,4,1,, [497, 2182, 583, 2241], false, false,

        return  service.waitForPathAndClick(intArrayOf(0,0,4),10*1000)
    }

    private fun clickBuyMenuItem(service: TradeAccessibilityService):Boolean{
        // 3. 自动点击【买入】菜单
        // ______________0, null, android.view.View, [], 0,0,2,0,0,0,0,, [24, 278, 270, 569], true, false,
        // ________________0, null, android.widget.Image, [img_jy_buy], 0,0,2,0,0,0,0,0,, [78, 323, 216, 461], false, false,
        // ________________1, null, android.view.View, [], 0,0,2,0,0,0,0,1,, [24, 278, 270, 569], false, false,
        // ________________2, null, android.widget.TextView, [买入], 0,0,2,0,0,0,0,2,, [102, 476, 189, 527], false, false,

        return  service.waitForPathAndClick(intArrayOf(0,0,2,0,0,0,0),10*1000)
    }

    private fun editTradeAccountNumber(service: TradeAccessibilityService):Boolean{
        // 4. 自动填充【登陆】信息
        // ______8, null, android.widget.EditText, [请输入交易账号], 0,0,8,, [114, 501, 921, 633], true, true,

        return service.waitForPathAndEdit(intArrayOf( 0,0,8),"9512085",10*1000)
    }
}